define(['jquery','api','user','util','text!pages/fund/cash.html','Vue'],function($,api,user,util,tpl,Vue){
	var vm,
	   statusHash={},
	   banks={},//银行列表
       channels={},//通道列表
       userchannels={},
       userchannelsTimer,
       banksTimer,
       channelsTimer,
       allowFundTimer,
	   lang={
	      0:'我要提现',
	      2:'微信授权',
	      1:'去绑银行卡'
	   },
	   controller = {
	   init:function(backUrl){
	   	   if(!user.checkLogin())return;
	   	   	api.headerShow(1);
			api.headerTitle(L('html_title_fund_cash'));
	   	   	api.headerLeftLink('#/user');
	   	   api.footerShow(0);//隐藏底部导航
		   api.setTitle(L('html_title_fund_cash'));
		   
		   this.render();
	   },
	   render:function(){
	   	var that=this;
	   		$('#common').html('<div id="cash"></div>');
			vm=new Vue({
           		el:'#cash',
	           data:{
	           	  show:true,
	           	  loading:false,
	           	  allowfunds:'--',
	           	  outChannel:'',
	           	  canCash:false,
	           	  outMoney:'',
	           	  pwd:'',
	           	  channels:false,
	           	  buttonValue:'',
	           	  banks:[]
	           },
	           watch:{
	              outChannel:function(v){
	              	 var status=statusHash[v],
	              	     str=lang[status];
	              	 vm.canCash=status==0;
	              	 vm.buttonValue=str;
	              }
	           },
	           filters:{
	               priceFormat:function(v){
	                  if(isNaN(v)){return v='--'}; // 20161209dyx
	                  return util.priceFormat(2,v);
	               }
	    	   },
	           methods:{
	           	  moneyChange:function(event,max){
	           	     var e=event.target,
	          		    d=$(e),
	          		    v=d.val(),
	          		    v2=v.replace(/[^\d\.]/g, ""),
	          		    i=v2.indexOf("."),
	              		    len=e.selectionEnd;
	  
	              		v2&&i > -1 &&v2.length - i> 3&&(v2=v2.substring(0, len - 1)+v2.substr(len));
	              		v2&&v2.split('.').length>2&&(v2=v2.substring(0, len - 1)+v2.substring(len));
	          		v2&&'0'==v2[0]&&/^[0-9]$/.test(v2[1])&&(v2=v2.substring(1));
	          		Number(v2)>max&&(v2=max.toFixed(2));
	          		this.outMoney=v2
		          },
	              toCash:function(){
	                 var data={
	                	   amount:this.outMoney*100,
	                	   channel:this.outChannel,
	                	   moneypwd:this.pwd
	                	},status=statusHash[this.outChannel];
	                	if(this.canCash){
	                		if(data.amount==''){
	                		   util.msg('输入金额格式不正确！');
	                		   return;
	                		}else if(data.moneypwd==''){
	                		   util.msg('请输入密码！');
	                		   return;
	                		}else if(parseInt(data.amount)>this.allowfunds){
	                		   util.msg('提现金额不能大于账户余额！');
	                		   return
	                		}
	                	    api.cash(data,function(rs){
	                	        if(rs.retcode=='0'){
	                	            util.msg('提现申请成功！');
	                	            vm.pwd='';
	                	            vm.outMoney='';
	                	            setTimeout(function(){history.go(-1)},500)
	                	        }else{
	                	            util.dealErrorCode(rs.retcode);
	                	        }
	                	    });
	                	}else if(status==1){
	                		util.msg('跳转中');
	           	    		setTimeout(function(){location.hash='#/fund/bind/'+data.channel+'/'+encodeURIComponent(location.href);},500);
	                	}else if(status==2){
	                	
	                	}
	              }
	           },
	           template:tpl
        	});
			
			vm.loading=true;
	   		that.loadAllowFund();
	   },
	   loadAllowFund:function(){//资金显示
	   		var that=this,
	   			cb=function(){
	   				that.loadAllowFund();
	   			};
	   			
	      	api.allowFund(
		      	function(rs){
		          if (rs.retcode=='0' && rs.data) {
		              vm.allowfunds=rs.data.allowoutfunds;
		              that.getUserInfo();
		          }else{
		          	allowFundTimer = setTimeout(cb,2000);
		          }
		      	},
		      	function(){
		      		allowFundTimer = setTimeout(cb,2000);
		      	}
      		);
	   },
	   initChannel:function(){
	   		vm.loading=false;
	   	   var 
	   	       channelHash={},
	   	       channel,
	   	       channelid,
	   	       userchannel,
	   	       temp=[],
	   	       cb=function(){
	   	       	  for(var i=0;i<userchannels.length;i++){
	   	       	  	   channel=userchannels[i];
	   	       	  	if (channel.isprimary==='49'){
		   	       	       channelHash[channel.channelid]=channel;
	   	       	  	   }
	   	       	  }
	   	       	  for(var i=0;i<channels.length;i++){
	   	       	  	   channel=channels[i];
	   	       	       if(+channel.outmode>50 || channel.channelid==48){
		   	       	  	  channelid=channel.channelid;
		   	       	  	  userchannel=channelHash[channelid];
		   	       	  	  if(userchannel){
		   	       	  	  	  channel["bankid"]=userchannel.bankid;
	                      	  channel.bankaccount=userchannel.bankaccount;
	                      	  channel.bank=userchannel.bankid&&banks[userchannel.bankid] ? banks[userchannel.bankid] : '';
		                      statusHash[channelid]=channel.bankaccount?0:1;
		   	       	       	  temp.push(channel);
		   	       	  	  }
	   	       	       }
	   	       	  }
	           	  vm.channels=temp;
	   	      };
	       
	       if(banks && userchannels && channels)cb();
	   },
	   getUserInfo:function(){
	   		var that=this,
	   			cb=function(){
	   				that.getUserInfo();
	   			};
	   			
   			api.userInfo(
   				function(rs){
			   	   if(rs.retcode=='0'){
			   	   		vm.userfunds=rs.data;
			   	   		userchannels=rs.data.channel?rs.data.channel:[];
			   	   		that.getBanks();
			   	   }else{
			   	      util.msg('用户未登录或超时！');
			   	      userchannelsTimer = setTimeout(cb,2000);
			   	   }
			   },
			   function(){//err
			   		userchannelsTimer = setTimeout(cb,2000);
			   }
			);
	   },
	   getBanks:function(){
	   		var that=this,
	   			cb=function(){
	   				that.getBanks();
	   			};
	   		// 获取银行列表
       	  	api.banks(
       	  		function(rs){
				   if(rs.retcode=='0' && rs.data){
					banks = rs.data;
					that.getChannels();
				   }else if(rs.retcode=='43'){
				       util.msg('系统缓存服务bug,请联系管理员！')
				       banksTimer = setTimeout(cb,2000);
				   }else{
				   		banksTimer = setTimeout(cb,2000);
				   }
				},
				function(){//err
					banksTimer = setTimeout(cb,2000);
				}
   	  		);
	   },
	   getChannels:function(){
	   		var that=this,
	   			cb=function(){
	   				that.getChannels();
	   			};
	   			
	   		api.channels(
	   			function(rs){
		           if(rs.retcode=='0' && rs.data){
		           	  channels=rs.data;
		           	  that.initChannel();
		           }else{
		           		channelsTimer = setTimeout(cb,2000);
		           }
		       },
		       function(){//err
		       		channelsTimer = setTimeout(cb,2000);
		       }
   			);
	   },
	   cleartFullTimer:function(){
		   	if(channelsTimer) clearTimeout(channelsTimer);
		   	if(banksTimer) clearTimeout(banksTimer);
		   	if(channelsTimer) clearTimeout(channelsTimer);
		   	if(userchannelsTimer) clearTimeout(userchannelsTimer);
		   	if(allowFundTimer) clearTimeout(allowFundTimer);
	   },
	   onRouteChange:function(){
	   	if(vm)vm.show=false;
	   	this.cleartFullTimer();
	   }
	};
    return controller;
})